package com.stoneframe.controller.manageSystem;

import java.util.List;

import javax.annotation.Resource;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.stoneframe.controller.base.BaseGenericController;
import com.stoneframe.service.BaseService;
import com.stoneframe.service.manageAuthorize.SysAuthorizeDataService;
import com.stoneframe.service.manageSystem.SysDepartmentService;
import com.stoneframe.service.manageSystem.SysUserService;
import com.stoneframe.util.baseApp.AppHelper;
import com.stoneframe.util.baseApp.PageData;
import com.stoneframe.util.baseApp.ToolHelper;

@Controller
@RequestMapping(value="sysUser")
public class SysUserController extends BaseGenericController {
	
	@Resource(name="sysUserService")
	private SysUserService sysUserService;
	
	@Resource(name="sysDepartmentService")
	private SysDepartmentService sysDepartmentService;
	
	@Resource(name="sysAuthorizeDataService")
	private SysAuthorizeDataService sysAuthorizeDataService;

	@Override
	public BaseService getService() {
		// TODO Auto-generated method stub
		return sysUserService;
	}
	
	/**
	 * 下拉选项
	 * */
	@ResponseBody
	@RequestMapping(value="comboBox")
	public Object comboBox(){
		logBefore(logger, className+"comboBox");
		
		PageData pd=this.getPageData();
		List<PageData> list=null;
		try{
			list=sysUserService.listAll(pd);
			logAfter(logger);
			return AppHelper.returnObject(pd,list);
		}catch(Exception e){
			logger.error(e.toString(),e);
			return AppHelper.errorObject(e.getMessage());
		}
		
	}
	
	/**
	 * 更新记录
	 */
	@ResponseBody
	@RequestMapping(value="edit")
	@Override
	public Object edit() {
		// TODO Auto-generated method stub
		logBefore(logger, className+".edit");
		
		PageData pd=this.getPageData();
		
		try{
			if(ToolHelper.notEmpty(pd.getString("keyValue"))){
				getService().edit(pd);
			}else{
				pd.put("keyValue", this.get32Guid());
				pd.put("userPwd", new SimpleHash("SHA-1",pd.getString("userName"),pd.getString("userPwd")).toString());
				getService().save(pd);
			}
			return AppHelper.successObject("操作成功！");
		}catch(Exception e){
			logger.error(e.getMessage(), e);
			return AppHelper.errorObject("操作失败！"+e.getMessage());
		}
	}

	/**
	 * 修改密码
	 * */
	@ResponseBody
	@RequestMapping(value="editUserPwd")
	public Object editUserPwd(){
		logBefore(logger, className+"editUserPwd");
		
		PageData pd=this.getPageData();
		try{
			pd.put("userPwd", new SimpleHash("SHA-1",pd.getString("userName"),pd.getString("userPwd")).toString());
			sysUserService.editUserPwd(pd);
			logAfter(logger);
			return AppHelper.successObject("修改成功");
		}catch(Exception e){
			logger.error(e.toString(),e);
			return AppHelper.errorObject(e.getMessage());
		}
	}
	
	/**
	 * 禁用
	 * */
	@ResponseBody
	@RequestMapping(value="disabled")
	public Object disabled(){
		logBefore(logger, className+"disabled");
		
		PageData pd=this.getPageData();
		try{
			pd.put("isEnabled", 0);
			sysUserService.editEnabled(pd);
			
			logAfter(logger);
			return AppHelper.successObject("禁用成功");
		}catch(Exception e){
			logger.error(e.toString(),e);
			return AppHelper.errorObject(e.getMessage());
		}
	}
	
	/**
	 * 启用
	 * */
	@ResponseBody
	@RequestMapping(value="enabled")
	public Object enabled(){
		logBefore(logger, className+"enabled");
		
		PageData pd=this.getPageData();
		try{
			pd.put("isEnabled", 1);
			sysUserService.editEnabled(pd);
			
			logAfter(logger);
			return AppHelper.successObject("禁用成功");
		}catch(Exception e){
			logger.error(e.toString(),e);
			return AppHelper.errorObject(e.getMessage());
		}
	}
	
	@ResponseBody
	@RequestMapping(value="getAuthorUserId")
	public Object getAuthorUserId(){
		logBefore(logger, className+"getAuthorUserId");
		try{
			String result=sysAuthorizeDataService.getDataAuthorUserId();
			
			logAfter(logger);
			return AppHelper.successObject(result);
		}catch(Exception e){
			logger.error(e.toString(), e);
			return AppHelper.errorObject(e.getMessage());
		}
	}

}
